package gfx;

import javax.microedition.khronos.opengles.GL10;

import util.GameVars;
import util.Logger;
import util.VertexBuffer;
import android.graphics.PointF;

public class GUI {
	private PressableButton leftArrow;
	private PressableButton rightArrow;
	
//	private Image controlBox;
	private Image controlCircle;
	
	private PointF controlCenter;
	private PointF touchPoint;

	private float[] controlBoxVertexes = new float[12];
	private float[] topControlBoxVertexes = new float[12];
	
	private int buttonNum;
	
	private boolean isMoved;
	private boolean isRight;
	
	public GUI(){
		load((short) 1);
	}
	
	public void load(short layout){
		leftArrow = new PressableButton(GameVars.BUTTON_TYPE_MOVE_LEFT, null, new PointF(1.5f, 1.5f), 1.5f, 1.5f, GameVars.BUTTON_NUM_MOVE_LEFT);
		rightArrow = new PressableButton(GameVars.BUTTON_TYPE_MOVE_RIGHT, null, new PointF(4.5f, 1.5f), 1.5f, 1.5f, GameVars.BUTTON_NUM_MOVE_RIGHT);
		
		touchPoint = new PointF(-1, -1);
		controlCenter = new PointF(3, 1.5f);
		
		isMoved = false;
		
		buttonNum = -1;
		
//		controlBox = new Image(GameVars.TEXTURE_CONTROL_BOX, 6, 3);
		controlCircle = new Image(GameVars.TEXTURE_CONTROL_CIRCLE, 2, 2);
	}
	
	public void update(long currentTime){
		
	}
	
	public void draw(GL10 gl){
		drawControlBox(gl);
		leftArrow.draw(gl);
		rightArrow.draw(gl);
	}
	
	private void drawControlBox(GL10 gl){
		// Standard Box Draw.
		// TODO Change 3 to a variable somewhere.
		controlBoxVertexes[0] = 0;
		controlBoxVertexes[1] = 0;
		controlBoxVertexes[2] = 0;
		controlBoxVertexes[3] = 0;
		controlBoxVertexes[4] = 3;
		controlBoxVertexes[5] = 0;
		controlBoxVertexes[6] = Camera.GL_WIDTH;
		controlBoxVertexes[7] = 0;
		controlBoxVertexes[8] = 0;
		controlBoxVertexes[9] = Camera.GL_WIDTH;
		controlBoxVertexes[10] = 3;
		controlBoxVertexes[11] = 0;
		
		topControlBoxVertexes[0] = 0;
		topControlBoxVertexes[1] = 9;
		topControlBoxVertexes[2] = 0;
		topControlBoxVertexes[3] = 0;
		topControlBoxVertexes[4] = 10;
		topControlBoxVertexes[5] = 0;
		topControlBoxVertexes[6] = Camera.GL_WIDTH;
		topControlBoxVertexes[7] = 9;
		topControlBoxVertexes[8] = 0;
		topControlBoxVertexes[9] = Camera.GL_WIDTH;
		topControlBoxVertexes[10] = 10;
		topControlBoxVertexes[11] = 0;
		
		gl.glPushMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(controlBoxVertexes));

		gl.glColor4f(0, 0, 0, 1);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, controlBoxVertexes.length / 3);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(topControlBoxVertexes));

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, topControlBoxVertexes.length / 3);
		
		gl.glColor4f(1, 1, 1, 1);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glPopMatrix();		

		// It doesn't fit in right now... I'll have to redraw it or just scrap it
//		controlBox.draw(gl, controlCenter);
		controlCircle.draw(gl, controlCenter);
	}

	// TODO Find a way to do something with this.
	public void handleUp(PointF location) {
		if(buttonNum == GameVars.BUTTON_NUM_MOVE_LEFT)
			if(leftArrow.isPressed(location)){
				isMoved = true;
				isRight = false;
			}
		if(buttonNum == GameVars.BUTTON_NUM_MOVE_RIGHT)
			if(rightArrow.isPressed(location)){
				isMoved = true;
				isRight = true;
			}
		touchPoint.set(-1, -1);
		buttonNum = -1;
	}

	public void handleDrag(PointF location) {
		
	}

	public boolean handleTouch(PointF location) {
		touchPoint.set(location.x, location.y);
		if(leftArrow.isPressed(touchPoint)){
			buttonNum = leftArrow.getNum();
			return true;
		}
		else if(rightArrow.isPressed(touchPoint)){
			buttonNum = rightArrow.getNum();
			return true;
		}
		return false;
	}
	
	public boolean isMoved(){ return isMoved; }
	/** @return isRight */
	public boolean handleMoved(){ isMoved = false; return isRight; }
}
